package com.cxmx.escort.base;

import com.cxmx.escort.common.redis.RedisUtils;
import com.cxmx.escort.service.*;
import com.github.binarywang.wxpay.service.WxPayService;
import org.springframework.beans.factory.annotation.Autowired;
import sun.misc.BASE64Encoder;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;

/**
 * @ClassName:BaseController
 * @Description //TODO
 * @Author chenyunxuan
 * @Date $ $
 **/
public class BaseController {
    @Autowired
    protected WxPayService wxService;
    @Autowired
    protected IUserService userService;
    @Autowired
    protected ITopService topService;
    @Autowired
    protected IFeedBackService feedBackService;
    @Autowired
    protected IBedService bedService;
    @Autowired
    protected IBedOrderService bedOrderService;
    @Autowired
    protected IMalfunctionService malfunctionService;
    @Autowired
    protected IRegionService regionService;
    @Autowired
    protected ISalesmanService salesmanService;

    protected Object getUserId(String token) {
        return RedisUtils.get("user:login:" + token);
    }

    protected Long getLongTypeUserId(String token) {
        return Long.parseLong(String.valueOf(getUserId(token)));
    }

    protected String getStringTypeUserId(String token) {
        return String.valueOf(getUserId(token));
    }

    protected String generateToken() {
        String token = System.currentTimeMillis() + new Random().nextInt() + "";
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance("md5");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        byte[] md5 = md.digest(token.getBytes());
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(md5);
    }
}
